<?php
/**
 * 订单接口
 * @author maniac
 */
namespace app\api\controller\v1;

use app\facade\PointOrders AS PointOrdersFacade;

class PointOrders extends Base
{
    /**
     * 确认订单
     */
    public function confirmOrder(){
        $post = input('post.');
        $goods =input('goods','');
        $buy_id =input('buy_id','');
        $post['buyType'] =input('buyType',1);
        $post['userid'] = $this->checkUserToken();

        if (empty($goods) && empty($buy_id)) {
            return j(1, '请选择要购买的商品');
        }

        try {
            $data = PointOrdersFacade::confirmOrder($post);
            return j(0, '获取成功', $data);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }

    /**
     * 添加订单
     */
    public function add()
    {
        $post = input('post.');
        
        $post['userid'] = $this->checkUserToken();
       
        if (!(int)$post['user_address_id']) {
        	
            return j(1, '请选择收货地址');
            
        }
        
        try {
        	
            $data = PointOrdersFacade::add($post);
            
            return j(0, '添加订单成功', $data);
            
        } catch (\Exception $e) {

            return j(1, $e->getMessage());

        }
        
    }

     /**
     * 删除订单
     */
    public function del(){
    	
        $id = input('post.id/d', 0);
        
        $userid = $this->checkUserToken();

        try {
        	
            $data = PointOrdersFacade::del($id,$userid);
            
            return j(0, '删除订单成功', $data);
            
        } catch (\Exception $e) {
        	
            return j(1, $e->getMessage());
            
        }
        
    }

    /**
     * 获取订单列表
     */
    public function orderList()
    {
        $userId     = $this->checkUserToken();
        
        $tabIndex   = input('post.tabIndex/d', 0);
        
        $page       = input('post.page/d', 1);
        
        $limit      = input('post.limit/d', 10);
        
        // 分页偏移量
        $offset     = ($page - 1) * $limit;
        
        $limit      = $offset.','.$limit;


        try {
        	
            $data = PointOrdersFacade::orderList($userId, $limit, $tabIndex);
            return j(0, '获取订单信息成功', $data);
            
        } catch (\Exception $e) {

            return j(1, $e->getMessage());

        }
        
    }
    
    
    /**
     * 获取订单详情
     */

    public function payOrder()
    {
    	
        $userId = $this->checkUserToken();
        
        $id     = input('post.id/d', 0);
        
        try {
        	
            $data = PointOrdersFacade::orderInfo($id, $userId);
            
            return j(0, '获取订单信息成功', $data);
            
        } catch (\Exception $e) {
        	
            return j(1, $e->getMessage());
            
        }
        
    }
    
    /**
     * 确认收货
     */
    public function confirmGoods()
    {
    	
        $userId = $this->checkUserToken();
        
        $id     = input('post.id/d', 0);
        
        try {
        	
            $result = PointOrdersFacade::confirmGoods($userId, $id);
            
            if ($result) {
            	
                return j(0, '确认收货成功');
                
            }
            return j(1, '确认收货失败');
            
        } catch (\Exception $e) {
        	
            return j(1, $e->getMessage());
            
        }
    }
    
    /**
     * 订单支付
     */
    public function pay_order()
    {
    	 
    	$userId = $this->checkUserToken();
    
    	$id     = input('post.id/d', 0);
    
    	try {
    		 
    		$result = PointOrdersFacade::pay_order($userId, $id);
    
    		if ($result) {
    			 
    			return j(0, '支付成功');
    
    		}
    		return j(1, '失败');
    
    	} catch (\Exception $e) {
    		 
    		return j(1, $e->getMessage());
    
    	}
    }
}
